<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="SKU名称">
        <el-input v-model="skuParams.skuName" placeholder="SKU名称" />
      </el-form-item>

      <el-form-item label="价格(元)">
        <el-input v-model="skuParams.price" type="number" placeholder="价格(元)" />
      </el-form-item>

      <el-form-item label="重量(克)">
        <el-input v-model="skuParams.weight" type="number" placeholder="重量(克)" />
      </el-form-item>

      <el-form-item label="SKU描述">
        <el-input v-model="skuParams.skuDesc" type="textarea" placeholder="SKU描述" />
      </el-form-item>

      <el-form-item label="平台属性">
        <el-form label-width="80px" :inline="true">
          <el-form-item v-for="(item, _index) in attrArr" :key="item.id" :label="item.attrName">
            <el-select v-model="item.attrIdAndValueId" style="width: 200px" :placeholder="`请选择${item.attrName}`" clearable filterable>
              <el-option v-for="(attrValue, _index) in item.attrValueList" :key="attrValue.id" :label="attrValue.valueName" :value="`${item.id}:${attrValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="销售属性">
        <el-form label-width="80px" :inline="true">
          <el-form-item v-for="(item, _index) in saleArr" :key="item.id" :label="item.saleAttrName">
            <el-select v-model="item.saleIdAndValueId" style="width: 200px" :placeholder="`请选择${item.saleAttrName}`" clearable filterable>
              <el-option v-for="saleAttrValue in item.spuSaleAttrValueList" :key="saleAttrValue.id" :label="saleAttrValue.saleAttrValueName" :value="`${item.id}:${saleAttrValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>

      <el-form-item label="图片名称">
        <el-table ref="table" border stripe :data="imgArr">
          <el-table-column type="selection" width="80" align="center" />
          <el-table-column label="图片">
            <template #default="{ row }">
              <img :src="row.imgUrl" style="height: 100px; width: 100px" />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button :type="skuParams.skuDefaultImg === row.imgUrl ? 'warning' : 'primary'" size="small" @click="handler(row)">{{ skuParams.skuDefaultImg === row.imgUrl ? '默认图片' : '设置默认' }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <el-form-item>
        <el-button :loading="loading" type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import type { SkuData, SpuData, SaleAttrResponseData, SpuHasImg, SpuImg, SaleAttr } from '@/api/product/spu/type';
import type { AttrResponseData, AttrList } from '@/api/product/attr/type';

import { reqAttr } from '@/api/product/attr';
import { reqSpuImageList, reqSpuHasSaleAttr, reqAddSku } from '@/api/product/spu';
import { reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
let $emit = defineEmits(['changeScene']);

const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' });
};
//平台属性
let attrArr = ref<AttrList>();
//销售属性
let saleArr = ref<SaleAttr[]>();
//照片数据
let imgArr = ref<SpuImg[]>();
// 收集SKU的参数
let skuParams = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '',
});

//获取table组件
let table = ref();

const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: SpuData) => {
  skuParams.category3Id = spu.category3Id;
  skuParams.spuId = spu.id as number;
  skuParams.tmId = spu.tmId;
  let result: AttrResponseData = await reqAttr(c1Id, c2Id, spu.category3Id);
  let result2: SpuHasImg = await reqSpuImageList(spu.id as number);
  let result3: SaleAttrResponseData = await reqSpuHasSaleAttr(spu.id as number);

  attrArr.value = result.data;
  imgArr.value = result2.data;
  saleArr.value = result3.data;
};

//设置默认图片
const handler = (row: any) => {
  //先取消所有勾选选项
  table.value.clearSelection();
  //选中复选框
  table.value.toggleRowSelection(row, true);
  skuParams.skuDefaultImg = row.imgUrl;
};

let loading = ref<boolean>(false);

//保存按钮的方法
const save = async () => {
  if (loading.value) return;
  loading.value = true;
  skuParams.skuAttrValueList = attrArr.value?.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':');
      prev.push({
        attrId,
        valueId,
      });
    }
    return prev;
  }, []);
  skuParams.skuSaleAttrValueList = saleArr.value?.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      let [saleId, valueId] = next.saleIdAndValueId.split(':');
      prev.push({
        saleAttrId: saleId,
        saleAttrValueId: valueId,
      });
      return prev;
    }
  }, []);
  let result: any = await reqAddSku(skuParams);
  if (result.code === 200) {
    ElMessage({
      message: '添加成功',
      type: 'success',
    });
    cancel();
  } else {
    ElMessage({
      message: '添加失败',
      type: 'error',
    });
  }
  loading.value = false;
};

defineExpose({ initSkuData });
</script>

<style lang="scss" scoped></style>
